package com.example.csv_demo.csvUtils;


import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author: WuYuehui
 * @create: 2021/12/23
 * @Description:
 * @FileName: CSVUtils
 * @History:
 * @自定义内容：
 */
public class CSVUtils {

    public static List<String[]> read(String readPath, String[] newHeaderCollection, String[] keyList) {

        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(readPath);
            boolean qualified = csvReader.isQualified(1);
            if (qualified) {
                return null;
            }
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                // 读一整行
                System.out.println(csvReader.getRawRecord());
                String rawRecord = csvReader.getRawRecord();
                // 读这行的某一列
                String link = csvReader.get("type");
                System.out.println(csvReader.get("type"));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void write(String writePath, String[] newHeaderCollection,List<String[]> writeDataCollection){

        String filePath = "/Users/dddd/test.csv";

        try {
            // 创建CSV写对象
            CsvWriter csvWriter = new CsvWriter(filePath,',', Charset.forName("GBK"));
            //CsvWriter csvWriter = new CsvWriter(filePath);

            // 写表头
            String[] headers = {"编号","姓名","年龄"};
            String[] content = {"12365","张山","34"};
            csvWriter.writeRecord(headers);
            csvWriter.writeRecord(content);
            csvWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String pathPrefix="F:\\数据\\站点_20140513-20141231\\china_sites_";
        String pathSuffix = ".csv";
        String fileName = "";
//        文件名称推算

        String readPath=pathPrefix+fileName+pathSuffix;
        System.out.println(readPath);
        String[] newHeaderCollection={};
        String[] keyLisst={};
//        read(filePath,null, null);
    }
}
